<template>
  <section class="add">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="面试人">
        <el-select v-model="form.studentId" placeholder="请选择">
          <el-option v-for="item in this.stuPagination.records" :key="item.studentId" :label="item.studentName"
            :value="item.studentId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="面试官">
        <el-select v-model="form.adminId" placeholder="请选择">
          <el-option v-for="item in this.adminPagination" :key="item.adminId" :label="item.adminName" :value="item.adminId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="面试日期">
        <el-col :span="24">
          <el-date-picker placeholder="选择日期" v-model="form.date" style="width: 100%;"></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">立即创建</el-button>
        <el-button type="text" @click="cancel()">取消</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>

<script>
export default {
  data() {
    return {
      form: { //表单数据初始化
        adminId: null,
        studentId: null,
        date: '',
        status: 0, // 0创建成功 1面试完毕
        remasks: ''
      },
      stuPagination: {
        //分页后的考试信息
        current: 1, //当前页
        total: null, //记录条数
        size: 20, //每页条数
        records: []
      },
      adminPagination: []
      // studentId: null,
      // adminId: null
    };
  },
  created() {
    this.getStudent()
    this.getAdmin()
  },
  methods: {
    formatTime(date) { //日期格式化
      let year = date.getFullYear()
      let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
      let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
      let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      // 拼接
      return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
    },
    onSubmit() {
      let interviewDate = this.formatTime(this.form.date)
      this.form.date = interviewDate.substr(0, 10)
      console.log(this.form);
      this.$axios({
          url: '/api/interview',
          method: 'post',
          data: {
            ...this.form
          }
        }).then(res => {
          if (res.data.code == 200) {
            this.$message({
              message: '数据添加成功',
              type: 'success'
            })
            this.$router.push({ path: '/selectInterview' })
          }
        })
    },
    getStudent() {
      this.$axios(`/api/students/${this.stuPagination.current}/${this.stuPagination.size}`).then(res => {
        this.stuPagination = res.data.data;
      }).catch(error => { });
    },
    getAdmin() {
      this.$axios('/api/admins').then(res => {
        this.adminPagination = res.data.data;
        console.log(this.adminPagination);
      }).catch(error => { });

    },
    cancel() { //取消按钮
      this.form = {}
    },

  }
};
</script>

<style lang="scss" scoped>
.add {
  padding: 0px 40px;
  width: 400px;
}
</style>